package com.sketch.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sketch.commom.PageResult;
import com.sketch.commom.Result;
import com.sketch.commom.utils.DateUtil;
import com.sketch.entity.GoldRecord;
import com.sketch.entity.dto.GoldRecordDTO;
import com.sketch.mapper.GoldRecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoldRecordService {
    @Resource
    private GoldRecordMapper goldRecordMapper;

    public Result list(String name, String startDate,String endDate, Long uid, Integer page, Integer size) {
        LambdaQueryWrapper<GoldRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!ObjectUtils.isEmpty(name),GoldRecord::getName, name)
                .eq(!ObjectUtils.isEmpty(uid),GoldRecord::getUid, uid)
                .between(!ObjectUtils.isEmpty(startDate) && !ObjectUtils.isEmpty(endDate),
                        GoldRecord::getCreateTime, DateUtil.getStart(startDate), DateUtil.getEnd(endDate));
        Page<GoldRecord> sketchInfoPage = new Page<>(page, size);
        IPage<GoldRecord> result = goldRecordMapper.selectPage(sketchInfoPage, queryWrapper);
        PageResult dtoPage = new PageResult<>();
        BeanUtils.copyProperties(result,dtoPage);
        List<GoldRecordDTO> list = new ArrayList<>();
        if(ObjectUtils.isEmpty(result.getRecords())){
            return Result.success(dtoPage);
        }
        result.getRecords().forEach(e->{
            GoldRecordDTO goldRecordDTO = new GoldRecordDTO();
            BeanUtils.copyProperties(e,goldRecordDTO);
            list.add(goldRecordDTO);
        });
        dtoPage.setList(list);
        return Result.success().withData(dtoPage);
    }
}
